
use Data/accountsales_complete.dta, clear

gen quarter = qofd(contract_start_date)
format quarter %tq


collapse (lastnm) Sales Consumer Renewal, by(account_id quarter)


merge 1:1 quarter account_id using Data/Individual_withsolar.dta


	
	
 
	* 3 quarters is nine months 
	local length = 3
		 
	forval t = 0(1)`length' {
	* `t' < 0 indicates in low subsidy period relative to the event

		gen event_after`t' =  SHCP_end ==`t' |  PFIT_starttime==`t' | PFIT_endtime==`t' | ///
		 SRES1_end ==`t' | SRES2_end==`t' | SRES_TFIT_endtime==`t' | FFIT_endtime==`t'
		 
		 
		 gen event_before`t' =  SHCP_end ==-`t'  | PFIT_starttime==-`t' | PFIT_endtime==-`t' | ///
		 SRES1_end ==-`t' | SRES2_end==-`t' | SRES_TFIT_endtime==-`t' | FFIT_endtime==-`t'
		
		gen event_after_new`t' = event_after`t'*cum
		gen event_before_new`t' = event_before`t'*cum
		
		gen event_after_high`t' = event_after`t'*roofrelativeIV
		gen event_before_high`t' = event_before`t'*roofrelativeIV
		
		
		drop event_after`t'
		drop event_before`t'
		
		
		
		}
			
	
	label variable event_before_new3  "Event Period -3"
	label variable event_before_new2  "Event Period -2"
	label variable event_before_new1  "Event Period -1"
	label variable event_after_new0   "Event Period "
	label variable event_after_new1   "Event Period +1"
	label variable event_after_new2   "Event Period +2"
	label variable event_after_new3   "Event Period +3"


	drop  event_before_high0  event_before_new0 
		
	keep if roofrelativeIV<.




	xi: xtivreg2 green_p cuminstall  i.quarter   if Renewal ==0 , fe cluster(postcode) 

	
	
	
	quietly summ green_plan  if e(sample)
    local meandepvar=round(`r(mean)',.001)

	
	  

	 outreg2 using Analysis/Main_norenew, bdec(3) se  nocons label  ctitle("FE")  ///
		keep(event* cum  median_* Salesdriven Consumerdriven) addtext(Postcode FE, Yes, Year-Quarter FE, Yes, Mean dep var, 0`meandepvar') tex(fragment pretty) nonotes replace

	eststo model2c 
	
	
	
	xi: xtivreg2 green_p (cuminstall = roofrelativeIV)  i.quarter  if Renewal ==0   , fe cluster(postcode) 
	
	
	


	
	quietly summ green_plan  if e(sample)
    local meandepvar=round(`r(mean)',.001)

	
	  

	 outreg2 using Analysis/Main_norenew, bdec(3) se  nocons label  ctitle("IV")  ///
		keep(event* cum  median_* Salesdriven Consumerdriven) addtext(Postcode FE, Yes, Year-Quarter FE, Yes, Mean dep var, 0`meandepvar') tex(fragment pretty) nonotes append

	eststo model2c 













	xi: xtivreg2 green_p event_before_new3 event_before_new2 event_before_new1  event_after_new0 event_after_new1  event_after_new2 event_after_new3  ///
	  i.quarter    if Renewal ==0 ,  fe cluster(postcode)

	  
	  
	
	quietly summ green_plan  if e(sample)
    local meandepvar=round(`r(mean)',.001)

	
	  

	 outreg2 using Analysis/Main_norenew, bdec(3) se  nocons label  ctitle("FE")  ///
		keep(event* cum  median_* Salesdriven Consumerdriven) addtext(Postcode FE, Yes, Year-Quarter FE, Yes, Mean dep var, 0`meandepvar') tex(fragment pretty) nonotes append

	eststo model2c 







	xi: xtivreg2 green_p (event_before_new3 event_before_new2 event_before_new1  event_after_new0 event_after_new1  event_after_new2 event_after_new3  ///
	= event_after_high* event_before_high* )  i.quarter if Renewal ==0     , ffirst fe cluster(postcode)

	
	
	
	quietly summ green_plan  if e(sample)
    local meandepvar=round(`r(mean)',.001)

	

	 outreg2 using Analysis/Main_norenew, bdec(3) se  nocons label  ctitle("IV")  ///
		keep(event* cum   median_* Salesdriven Consumerdriven) addtext(Postcode FE, Yes, Year-Quarter FE, Yes, Mean dep var, 0`meandepvar') tex(fragment pretty) nonotes append

	eststo model2c 

